package com.czc.mvc.handler;

import com.czc.constant.CrowdConstant;
import com.czc.entity.Admin;
import com.czc.service.api.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class AdminHandler {

    @Autowired
    private AdminService adminService;

    //更新操作
    @RequestMapping("/admin/update.html")
    public String update(Admin admin,
                         @RequestParam("pageNum") Integer pageNum,
                         @RequestParam("keyword") String keyword
                         ){
        adminService.update(admin);
        return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }


    //根据ID更新页面的回显
    @RequestMapping("/admin/to/edit/page.html")
    public String toEditPage(
            @RequestParam(value = "adminId") Integer adminId,
            ModelMap modelMap
    ){
        //1.根据adminId查询Admin对象
        Admin admin=adminService.getAdminById(adminId);

        //2.将Admin对象存入模型
        modelMap.addAttribute("admin",admin);

        return "admin-edit";
    }

    @PreAuthorize("hasAuthority('user:save')")
    //新增
    @RequestMapping("/admin/save.html")
    public String save(Admin admin){
        adminService.saveAdmin(admin);
        return "redirect:/admin/get/page.html?pageNum="+Integer.MAX_VALUE;
    }

    //单个删除按钮
    @RequestMapping("/admin/remove/{adminId}/{pageNum}/{keyword}.html")
    public String remove(@PathVariable("adminId") Integer adminId,
                         @PathVariable("pageNum") Integer pageNum,
                         @PathVariable("keyword") String keyword
    ){
        //执行删除
        adminService.remove(adminId);

        //页面跳转 回到分页页面

        //直接转发到admin-page 无法显示分页的数据
        //return "admin-page";
        return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }

    @RequestMapping("/admin/get/page.html")
    public String getPageInfo(
            //使用RequestParam注解的defaultValue属性，指定默认值 在请求中没有携带对应参数是使用默认值
            //keyword默认值使用空字符串,和sql语句配合实现两种情况适配
            @RequestParam(value = "keyword",defaultValue = "") String keyword,

            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            ModelMap modelMap
    ){
        //调用service方法获取PageInfo对象
        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);

        //将pageInfo对象存入模型
        modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO,pageInfo);
        return "admin-page";
    }


    @RequestMapping("/admin/do/logout.html")
    public String doLogout(HttpSession session){

        //强制登入失效
        session.invalidate();

        return "redirect:/admin/to/login/page.html";
    }


    @RequestMapping("/admin/do/login.html")
    public String doLogin(@RequestParam("loginAcct") String loginAcct,
                          @RequestParam("userPswd") String userPswd,
                          HttpSession session){
        // 调用service方法执行登录检查
        // 这个方法如果能够返回admin对象说明登录成功,如果账号密码不正确则会抛出异常
        Admin admin = adminService.getAdminByLoginAcct(loginAcct, userPswd);

        // 将登入成功后返回的admin对象存入session域
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN,admin);

        //重定向
        return "redirect:/admin/to/main/page.html";
    }
}
